﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SundStudent.ModelLayer;
// 2012.12.03 Kis Boisen Hansen
// DBklasse til Stue

namespace SundStudent.DBLayer
{
    public  class DBStue
    {
     private SqlCommand dbCmd = null;

        public DBStue()
        {
        }

        public Stue FindStue(string stue)
        {
            string sql = @"select stuenr, beskrivelse from stue " +
                           " where stuenr  = '" + stue; 

            Console.WriteLine("sql stmt = " + sql);

           dbCmd = DBConnection.GetDbCommand(sql);

            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();
            Stue stueObj = new Stue();

            if (dbReader.Read())
            {
                try
                {
                    Console.WriteLine("antal felter " + dbReader.FieldCount);
                    stueObj = new Stue();
                    stueObj.Stuenr = dbReader["stuenr"].ToString();
                    stueObj.Beskrivelse = dbReader["beskrivelse"].ToString();
                              
                    Console.WriteLine("Stue objekt oprettet");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Stue objekt ikke oprettet korrekt");
                    stueObj = null;
                }


            }

            else
            {
                stueObj = null;
            }
            dbReader.Close();
            DBConnection.Close();
            return stueObj;
        }


        public List<Stue> GetAllStuer()
        {
            List<Stue> returnList = new List<Stue>();
            string sql = @"select stuenr, beskrivelse from stue ";
                         


            SqlCommand dbCmd = DBConnection.GetDbCommand(sql);

            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();

            Stue stueObj;

            while (dbReader.Read())
            {

                stueObj = new Stue();
                stueObj.Stuenr = dbReader["stuenr"].ToString();
                stueObj.Beskrivelse = dbReader["beskrivelse"].ToString();
                returnList.Add(stueObj);
            }
            DBConnection.Close();
            return returnList;

        }
    }
}


